package com.example.fraimwork;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;

public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void initContactCreation() {
		click(By.linkText("add new"));
	}

	public void fillContactForm(ContactData contact) {
		type(By.name("firstname"), contact.firstName);
		type(By.name("lastname"), contact.lastName);
		type(By.name("address"), contact.address);
		type(By.name("home"), contact.homeNumber);
		type(By.name("mobile"), contact.mobileNumber);
		type(By.name("work"), contact.workNumber);
		type(By.name("email"), contact.email);
		type(By.name("email2"), contact.email2);			
	    selectByText(By.name("bday"), contact.bday); 
	    selectByText(By.name("bmonth"), contact.bmonth);    	    
	    type(By.name("byear"), contact.byear);
	    //selectByText(By.name("new_group"), "group name 1");    	    
	    type(By.name("address2"), contact.address2);
	    type(By.name("phone2"), contact.phone2);	    
	}

	public void submitContactCreation() {
		click(By.name("submit"));
	}

	public void returnToHomePage() {
		click(By.linkText("home page"));
	}

	public void initContactEditing(int index) {
		click(By.xpath("//img[@alt='Edit'][" + index + "]"));
	}

	public void deleteContact() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	public void submitContactEditing() {
		click(By.xpath("(//input[@name='update'])[1]"));
	}

	public List<ContactData> getContacts() {
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement> elems = driver.findElements(By.xpath("//tr/td[2]"));
	
		for (int i=2; i<elems.size(); i++) {
			ContactData contact = new ContactData();
			contact.firstName = driver.findElement(By.xpath("//tr[" + i + "]/td[3]")).getText();
			contact.lastName = driver.findElement(By.xpath("//tr[" + i + "]/td[2]")).getText();
			contact.email = driver.findElement(By.xpath("//tr[" + i + "]/td[4]")).getText();
			contact.homeNumber = driver.findElement(By.xpath("//tr[" + i + "]/td[5]")).getText();
			contacts.add(contact);			
		}
		return contacts;
	}
	
	

}
